﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.Security;
using LiveWebServiceBase.ServiceUserSettings;

namespace LiveWebServiceAdmin
{
    public partial class Organizations : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            lbError.Visible = false;
            lbError.Text = "";

            string curuser = string.Empty;

            if (Settings.UserInfo.IsAdminUser(this.User.Identity.Name))
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                {
                    curuser = Page.Request.QueryString["manageuser"];
                }
                else
                    Page.Response.Redirect("/users.aspx");
            }
            else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) && Page.Request.QueryString["manageuser"] == this.User.Identity.Name)
                {
                    curuser = Page.Request.QueryString["manageuser"];
                }
                else
                    Response.Redirect("/Login.aspx");
            }

            sdklink.HRef = this.Request.Url.AbsoluteUri.Replace(this.Request.Url.PathAndQuery, "") + @"\LaeMWSSDK.zip";

            if (!string.IsNullOrEmpty(curuser))
            {
                hdManagedUser.Value = curuser;
                UserSettings usItem = new UserSettings(curuser);

                foreach (var item in usItem.GetOrganizations())
                {
                    lbUserOrganizations.Items.Add(item.orgname);
                }
            }
        }

        protected void btnDeleteOrg_Click(object sender, EventArgs e)
        {
            string username = string.Empty;
            string domain = string.Empty;
            string password = string.Empty;

            if (Session["userData"] != null)
            {
                string[] tmp = Authorization.DecryptString(Session["userData"].ToString()).Split(';');

                username = tmp[0];
                domain = (string.IsNullOrEmpty(tmp[1])) ? Environment.MachineName : tmp[1];
                password = tmp[2];
            }

            Impersonator imp = new Impersonator(username, domain, password);
            try
            {
                string curuser = string.Empty;

                if (Settings.UserInfo.IsAdminUser(this.User.Identity.Name))
                {
                    if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                    {
                        curuser = Page.Request.QueryString["manageuser"];
                    }
                    else
                        Page.Response.Redirect("/users.aspx");
                }
                else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                {
                    if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) &&
                        Page.Request.QueryString["manageuser"] == this.User.Identity.Name)
                    {
                        curuser = Page.Request.QueryString["manageuser"];
                    }
                    else
                        Response.Redirect("/Login.aspx");

                }
                if (lbUserOrganizations.SelectedItem != null)
                {
                    UserSettings usItem = new UserSettings(curuser);

                    usItem.DeleteOrganization(lbUserOrganizations.SelectedItem.Text);

                    Page.Response.Redirect(Page.Request.RawUrl);
                }
            }
            catch (Exception ex)
            {
                RequiredFieldValidator validator = new RequiredFieldValidator();
                lbError.ApplyStyle(validator.ControlStyle);
                lbError.Visible = true;
                lbError.Text = ex.Message;
            }
            finally
            {
                imp.Dispose();
            }
        }
    }
}
